<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import axios from "axios";
import {listEvent} from "@/api/volunteer/event";
import {listType} from "@/api/volunteer/type";

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default:'350px'
    }
  },
  data() {
    return {
      chart: null,
      allData: [],
      yData:[],
      yValue:[]
    }
  },
  mounted() {
    this.$nextTick(() => {
      //获取x坐标
      listEvent().then((response)=>{
          this.yData=response.rows.map((obj)=>{return obj.sysEventType.typeId})
        listType().then((response) => {
         this.allData = response.rows.map((obj) => {
           return obj.typeName
         })
           for(let i=0;i<this.allData.length;i++){
             let j=i+1
            this.yValue[i]=this.yData.filter((item)=>{return item==j}).length
           }
         this.initChart()
       })})

      },
      )},
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const option = {
        title:{
          show: true,
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data:this.allData,
          // data:['23','64','24','42','23'],
         axisLabel:{
            interval:0,
         },
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '活动个数',
          type: 'bar',
          stack: 'vistors',
          barCategoryGap:'50%',
          data: this.yValue,
          animationDuration
        }]
      }
      this.chart.setOption(option)
    },
    getYdata(){
      listEvent().then((response)=>{
        this.yData=response.rows.map((obj)=>{return obj.sysEventType.typeId})
        for(let i=0;i<this.allData.length;i++){
          let j=i+1
          this.yValue[i]=this.yData.filter((item)=>{return item==j }).length
        }
      })
    }

  }
}
</script>
